import { getFlatArr } from "@/utils/util";
import { defineStore } from "pinia";

export const AuthStore = defineStore({
	id: "AuthState",
	state: () => ({
		// 当前页面的 router name，用来做按钮权限筛选
		routeName: "",
		// 按钮权限列表
		authButtonList: {},
		// 菜单权限列表
		authMenuList: [],
		// 侧边栏权限列表
		authSidebarList: []
	}),
	getters: {
		authMenuListGet: state => state.authMenuList,
		flatMenuListGet: state => getFlatArr(state.authMenuList),
		authMenuSidebarListGet(state) {
			return state.authSidebarList;
		}
	},
	actions: {
		// getAuthMenuList
		async getAuthMenuList() {
			const { getRouters } = userApi();
			const { data } = await getRouters();
			this.authMenuList = data;
		},
		async getAuthButtonList() {
			const { getRoleSidebar } = userApi();
			const { data } = await getRoleSidebar();
			this.authButtonList = data;
		},
		async getAuthSidebarList() {
			const { getRoleSidebar } = userApi();
			const { data } = await getRoleSidebar();
			this.authSidebarList = data;
		},
		setRouteName(name) {
			this.routeName = name;
		}
	}
});
